<?php
/**
 *
 * Application for the function: View Schedule
 *
 * This page allows an employee to view his/her own schedule;
 * It also allows a manager to view all his/hers employees' schedule.
 *
 *
 * @author Chi Zhang
 * @version 1.0
 * @package dummy-auto
 *
 */

include('../config.inc');
require('dummy-app.inc');

function emp_type($empid) {
        $sql = mysql_query("
                select  Type
                  from  Employee
                 where  EmpID = $empid;
               ");

        if ($sql) {
                $row = mysql_fetch_array($sql);
                return $row[0];
        } else
                exit_404(mysql_error());
}

function get_dealer_num($empid) {
        $sql = mysql_query("
                select  DealerNum
                  from  Employee
                 where  EmpID = $empid;
               ");

        if ($sql) {
                $row = mysql_fetch_array($sql);
                return $row[0];
        } else
                exit_404(mysql_error());
}

session_start();

if ( ! ( session_id() && isset($_SESSION['empid'])
         && emp_type($_SESSION['empid']))    )
{
        $tree = make_app("View Schedule :: Invalid User!");
        $app = $tree->app;
        $app->addChild('p', "Only Authorized Employee Can Use This Function.");

        if (! ( session_id() && isset($_SESSION['empid'])) )
                session_end();

        echo $tree->asXML();
        exit;
}


$me = "select DaysInWeek,
              WorkHours
         from Employee_Info
        where EmpID = $_SESSION[empid];";

if (! ($sql_list_1 = mysql_query($me)) ) exit_404(mysql_error());
$num_rows_1 = mysql_num_rows($sql_list_1);


if (! $num_rows_1) {
        $tree = make_app("View Schedule :: Error!!!");
        $app = $tree->app;
        $app->addChild('p', "Error: Schedule is not found!");
} else {
        $tree = make_app("View Schedule");
        $app = $tree->app;
        $app->addChild('h2', "My Schedule:");
        $app->addChild('table');

        $tmp = array("Name", "Mon", "Tue", "Wed", "Thu", "Fri");
        $tmp_con = array("", "", "", "", "", "");

        $app->table->addChild('tr');
        for ($i=1; $i < 6; $i++)
                $app->table->tr->addChild('td', $tmp[$i]);

        for ($i=0; $i < $num_rows_1; $i++)
        {
                $row = mysql_fetch_array($sql_list_1);
                $tmp_con[$row[0]] .= $row[1];
        }

        $app->table->addChild('tr');
        for ($i=1; $i < 6; $i++)
                $app->table->tr[1]->addChild('td', $tmp_con[$i]);


        if (emp_type($_SESSION['empid']) == 'M') {

                $other = "select distinct EmpID,
                                          Name
                            from Employee
                           where EmpID != $_SESSION[empid]
                             and DealerNum = '" .
                        get_dealer_num($_SESSION['empid']) . "';";

                if (! ($sql_list_2 = mysql_query($other)) )
                        exit_404(mysql_error());
                $num_rows_2 = mysql_num_rows($sql_list_2);

                $app->addChild('h2', "Employees' Schedule:");
                $app->addChild('table');

                $app->table[1]->addChild('tr');
                for ($j=0; $j < 6; $j++)
                        $app->table[1]->tr->addChild('td', $tmp[$j]);

                for ($i=0; $i < $num_rows_2; $i++)
                {
                        $row_eid = mysql_fetch_array($sql_list_2);
                        $tmp_con = array("$row_eid[1]", "", "", "", "", "");

                        $one = "select DaysInWeek,
                                       WorkHours
                                  from Employee_Info
                                 where EmpID = $row_eid[0];";

                        if (! ($sql_list_3 = mysql_query($one)) )
                                exit_404(mysql_error());
                        $num_rows_3 = mysql_num_rows($sql_list_3);

                        for ($j=0; $j < $num_rows_3; $j++)
                        {
                                $row = mysql_fetch_array($sql_list_3);
                                $tmp_con[$row[0]] .= $row[1];
                        }

                        $app->table[1]->addChild('tr');
                        for ($j=0; $j < 6; $j++)
                                $app->table[1]->tr[$i+1]->addChild('td', $tmp_con[$j]);
                }

        }
}


echo $tree->asXML();
?>
